package com.logic.saas.util;

import org.apache.commons.lang3.StringUtils;
import com.logic.saas.enums.ConstantEnum;
import com.logic.saas.pojo.BasePojo;
import com.logic.saas.pojo.Ps;
import com.logic.saas.util.annotation.Column;
import java.lang.reflect.Field;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class SqlProvider<T extends BasePojo> {
	
	public String checkRecordExist(Ps ps) {
		String sql = "select 0";
		
		if(ps != null && ps.getParamMap() != null){
			String schema = ps.getDatabaseSchema();
			BasePojo obj = (BasePojo) ps.getParamMap().get(ConstantEnum.POJONAME.getValue()); 
			if(StringUtils.isNotBlank(schema) && obj != null){
				String tableName = obj.tablename();
				List<List<Field>> uniqueKeys = obj.uniqueKeys();
				if(StringUtils.isNotBlank(tableName) && uniqueKeys!=null && uniqueKeys.size()>0 && tableName.length() >0){
					sql ="select sum(n) from (";
					for(List<Field> uniqueKey : uniqueKeys){
						if(uniqueKey != null && uniqueKey.size() >0){
							String subSql =" select count(id) as n from "+schema+"."+tableName+" where ";
							for(Field field : uniqueKey){
								if(field != null){
									Object fieldValue = null;
									try {
										field.setAccessible(true);
										fieldValue = field.get(obj);
									} catch (IllegalArgumentException e) {
										e.printStackTrace();
									} catch (IllegalAccessException e) {
										e.printStackTrace();
									}
									String columnName = null, columnSource = null,columndateFormat = null;
									Column column = field.getAnnotation(Column.class);
									if(column != null){
										columnName =column.name();
										columnSource = column.source();
										columndateFormat = column.dateFormat();
									}
									if(fieldValue != null && StringUtils.isNotBlank(columnName)){
										if(StringUtils.isNotBlank(columndateFormat) && fieldValue instanceof Date){
											fieldValue = new SimpleDateFormat(columndateFormat).format(fieldValue);
										}
										if(StringUtils.isNotBlank(columnSource) && columnSource.contains(".")){
											String columns[] = columnSource.split("\\.");
											if(StringUtils.isNotBlank(columns[1])){
												try {
													BasePojo objField = (BasePojo)fieldValue;
													if(objField != null){
														Field f[] = objField.getClass().getFields();
														f = objField.getClass().getDeclaredFields();
														Field fieldbj = ReflectionUtils.getDeclaredField(objField, columns[1]);
														if(fieldbj != null){
															fieldbj.setAccessible(true);
															fieldValue = fieldbj.get(objField);
														}
													}
												} catch (Exception e) {
													e.printStackTrace();
												}
											}
										}
										subSql = subSql+ " "+columnName+" = '"+fieldValue+"'  and";
									}
								}
							}
							if(subSql.endsWith("and")){
								subSql = subSql.substring(0,subSql.length()-3);
							}
							sql = sql + subSql + "union all";
						}
						
					}
					if(sql.endsWith("union all")){
						sql = sql.substring(0, sql.lastIndexOf("union all"));
					}
					sql = sql + " )as t";
				}
			}
		}
		System.out.println("##"+sql);
		return sql;
    } 
	
	
	
	public static void main(String[] args) {

	}
}
